<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">公司编号</label>
        <el-input
          v-model="query.companySerial"
          clearable
          placeholder="公司编号"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">产品编号</label>
        <el-input
          v-model="query.productSerial"
          clearable
          placeholder="产品编号"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!-- <date-range-picker
          v-model="query.count"
          class="date-item"
        /> -->
        <label class="el-form-item-label">创建时间</label>
        <date-range-picker v-model="query.createTime" class="date-item" />
        <!-- <date-range-picker
          v-model="query.updateTime"
          start-placeholder="updateTimeStart"
          end-placeholder="updateTimeStart"
          class="date-item"
        /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="公司" prop="companySerial">
            <el-select
              v-model="form.companySerial"
              style="width: 370px"
              placeholder="请选择"
            >
              <el-option
                v-for="item in companyList"
                :key="item.serial"
                :label="item.name"
                :value="item.serial"
              />
            </el-select>
          </el-form-item>
          <!--
          <el-form-item label="产品编号" prop="productSerial">
            <el-input v-model="form.productSerial" style="width: 370px;" />
          </el-form-item> -->

          <el-form-item label="产品" prop="productSerial">
            <el-select
              v-model="form.productSerial"
              style="width: 370px"
              placeholder="请选择"
              filterable
            >
              <el-option
                v-for="item in productList"
                :key="item.serial"
                :label="item.name"
                :value="item.serial"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数量" prop="count">
            <el-input v-model="form.count" style="width: 370px" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
          >确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="id" /> -->
        <el-table-column prop="companySerial" label="公司编号" />
        <el-table-column prop="companyInfo.name" label="公司名称" />
        <el-table-column prop="productSerial" label="产品编号" />
        <el-table-column prop="productInfo.name" label="产品名称" />
        <el-table-column prop="count" label="数量" />
        <el-table-column prop="createTime" label="创建时间" />
        <!-- <el-table-column prop="updateTime" label="更新时间" /> -->
        <el-table-column
          v-if="
            checkPer(['admin', 'productionInfo:edit', 'productionInfo:del'])
          "
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProductionInfo from '@/api/productionInfo'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = {
  count: null,
  id: null,
  companySerial: null,
  productSerial: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'ProductionInfo',
  components: {
    pagination,
    crudOperation,
    rrOperation,
    udOperation,
    DateRangePicker
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '生产信息',
      url: 'api/productionInfo',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudProductionInfo }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'productionInfo:add'],
        edit: ['admin', 'productionInfo:edit'],
        del: ['admin', 'productionInfo:del']
      },
      rules: {
        count: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        companySerial: [
          { required: true, message: '公司编号不能为空', trigger: 'blur' }
        ],
        productSerial: [
          { required: true, message: '产品编号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'companySerial', display_name: '公司编号' },
        { key: 'productSerial', display_name: '产品编号' }
      ],
      companyList: [],
      productList: []
    }
  },
  mounted() {
    this.getComAction()
    this.getProductAction()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.afterRefresh]() {
      return true
    },
    getComAction() {
      crudProductionInfo
        .getCom()
        .then((res) => {
          this.companyList = res.content
          this.companyList.forEach((element) => {
            element.name = element.serial + '（' + element.name + '）'
          })
        })
        .catch(() => {})
    },
    getProductAction() {
      crudProductionInfo
        .getProduct()
        .then((res) => {
          this.productList = res.content
          this.productList.forEach((element) => {
            element.name = element.serial + '（' + element.name + '）'
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped>
</style>
